package com.example.backend.service.impl;

import com.example.backend.Requset.AddLogsRequest;
import com.example.backend.dao.LogsRepository;
import com.example.backend.dao.RoomRepository;
import com.example.backend.dao.UserRepository;
import com.example.backend.pojo.Logs;
import com.example.backend.pojo.Room;
import com.example.backend.pojo.User;
import com.example.backend.response.VisitedResponse;
import com.example.backend.service.LogsService;
import com.example.backend.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LogsServiceImpl implements LogsService {
    @Autowired
    private LogsRepository logsRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private RoomRepository roomRepository;
    @Override
    public Response<Logs> addEnterLogs(AddLogsRequest logs){
        User user=userRepository.findUserByUsername(logs.getUsername());
        if(user==null){
            return new Response<>(Response.FAIL,"no such user",null);
        }
        Room room=roomRepository.findRoomByName(logs.getRoom());
        if(room==null){
            return new Response<>(Response.FAIL,"no such room",null);
        }
        Logs userLog=logsRepository.findLogsByUserID(user.getId());
        if (userLog!=null){
            return new Response<>(Response.FAIL,"already in room",null);
        }
        room.setNum(room.getNum()+1);
        Logs log = new Logs(0, user.getId(), room.getId(), logs.getTime(), "0", true);
        logsRepository.save(log);
        roomRepository.save(room);
        return new Response<>(Response.SUCCESS,"success",null);
    }
    @Override
    public Response<Logs> addExitLogs(AddLogsRequest logs){
        User user=userRepository.findUserByUsername(logs.getUsername());
        if(user==null){
            return new Response<>(Response.FAIL,"no such user",null);
        }
        Room room=roomRepository.findRoomByName(logs.getRoom());
        if(room==null){
            return new Response<>(Response.FAIL,"no such room",null);
        }
        Logs log=logsRepository.findLogsByUserIDAndRoomID(user.getId(),room.getId());
        if(log==null){
            return new Response<>(Response.FAIL,"no in right room",null);
        }
        log.setExitTime(logs.getTime());
        log.setStatus(false);
        room.setNum(room.getNum()-1);
        logsRepository.save(log);
//        if (room.getNum() > 0){
            roomRepository.save(room);
//        }
//        else {
//            roomRepository.delete(room);
//        }
        return new Response<>(Response.SUCCESS,"success",null);
    }
    @Override
    public Response<List<VisitedResponse>> findAllVisited(String username){
        User user=userRepository.findUserByUsername(username);
        if(user==null){
            return new Response<>(Response.FAIL,"no such user",null);
        }
        List<Logs> allLogs=logsRepository.findAllLogsByUserID(user.getId());
        List<VisitedResponse> responses=new ArrayList<VisitedResponse>();
        for (int i=0;i<=allLogs.size()-1;++i){
            Logs log=allLogs.get(i);
            VisitedResponse response=new VisitedResponse(roomRepository.findRoomById(log.getRoomID()).getName(),log.getEnterTime());
            responses.add(response);
        }
        return new Response<>(Response.SUCCESS,"success",responses);
    }
}
